
/* JavaScript content from map/classes/Poly.js in folder common */
define(['dojo', 'map/classes/Utils'], function(dojo, Utils){
  return dojo.declare('map.classes.Poly', [], {
    colors: {
      RED : "rgba(200, 0, 0, 0.3)",
      DARKRED : "rgba(120, 0, 0, 0.5)",
      BRIGHTRED : "rgba(255, 0, 0, 0.9)",
      PINK : "rgba(255, 160, 160, 0.3)",
      YELLOW : "rgba(220, 220, 0, 0.2)",
      DARKYELLOW : "rgba(200, 200, 0, 0.3)",
      GREEN : "rgba(0, 120, 0, 0.3)",
      BRIGHTGREEN : "rgba(0, 255, 0, 0.9)",
      BLUE : "rgba(54, 255, 253 , 0.45)",
      BROWN : "rgba(139, 69, 19, 0.4)",
      WHITE : "rgba(255, 255, 255, 0.7)",
      GREY : "rgba(100,100, 100, 0.5)"
    },
    constructor: function(canvas, type, points, color, filled, fillcolor){
      this.canvas = canvas;
      this.context = canvas.context;

      this.type = type;
      this.points = points;     
      
      this.setColor( (color == null) ? 'BROWN' : color );

      this.filled = (filled == null || filled === true) ? true : false;
      this.setFillColor( (fillcolor == null) ? 'white' : fillcolor );

      this.finished = true;
      this.pointRadius = 5;
      // this.creationTithis = (new Date()).getTithis();
    },
    draw : function(context){
      if (!this.points || this.points.length === 0) { return; }

      // draw lines
      this.context.globalAlpha = 1.0;

      this.context.save();
      if (!this.filled) {
        this.context.lineWidth = 5;
        this.context.strokeStyle = this.color;
        this.context.fillStyle = this.color;
      } else {
        this.context.lineWidth = 1;
        //if (this.color == PolyColors.GREEN) {
        //  this.context.strokeStyle = this.color;
        //} else {
          this.context.strokeStyle = "#ffffff";
          this.context.fillStyle = this.fillColor;
        //}
      }
      

      this.context.beginPath();
      
      var start = this.canvas.geoToXY(this.points[0].lon, this.points[0].lat);

      this.context.moveTo(
        start.x,
        start.y
      );



      for (var i = 1; i < this.points.length; i++) {
        var pt = this.points[i]
          , pt2 = this.points[i+1];

        if (pt2){
          this.context.quadraticCurveTo(
            (this.canvas.geoToXY(pt.lon, pt.lat)).x,
            (this.canvas.geoToXY(pt.lon, pt.lat)).y,
            (this.canvas.geoToXY(pt2.lon, pt2.lat)).x,
            (this.canvas.geoToXY(pt2.lon, pt2.lat)).y
          );
        }          
        else{
          this.context.quadraticCurveTo(
            (this.canvas.geoToXY(pt.lon, pt.lat)).x,
            (this.canvas.geoToXY(pt.lon, pt.lat)).y,
            start.x,
            start.y
          );
        }

      }
      //if (this.finished == true) {
        this.context.closePath();
      //}
      if (this.filled) {
        this.context.fillStyle = this.fillColor;
        this.context.fill();
      }

      this.context.stroke();

      var weWantToDrawPoints = false;
      if (weWantToDrawPoints) {
        // draw points
        for (var i in this.points) {
          var pt = this.points[i];
          this.context.fillStyle = this.color;
          this.context.beginPath();
          this.context.arc(
            this.canvas.geoToXY(pt.lon, pt.lat).x,
            this.canvas.geoToXY(pt.lon, pt.lat).y,
            this.pointRadius, 0,
            Math.PI*2,
            true);
          this.context.closePath();
          this.context.fill();
          this.context.stroke();
        }
      }
      this.context.restore();
    },
    setColor: function(colorName){
      this.color = this.colors[colorName];
    },
    setFillColor: function(colorName){
      this.fillColor = this.colors[colorName];
    },
    isValid : function() {
      var circ = 0;
      var start = null;
      for (var i in this.points) {
        var xy = this.canvas.geoToXY(this.points[i]);
        if (start) {
          circ += this.canvas.getXYDist(start, xy);
        } 
        start = xy;
      }
      return (circ > 100) ? true : false;
    },    
    simplify : function(targetNum) {
      targetNum = targetNum || this.points / 2;

      this.simplePoints = [];


      if (targetNum > this.points.length) { 
        this.simplePoints = this.points;
        return this; 
      }
      
      for (var i = 0; i < targetNum; i++) {
        var index = Math.floor(i * (this.points.length / (targetNum-1)));
        if (index > this.points.length - 1) {
          index = this.points.length - 1;
        }
        this.simplePoints.push(this.points[index]);
      }

      return this;

    },
    isInside : function(map, pt) {
      var pts = [], i, start, end;
      
      this.simplify(25);

      for (i in this.simplePoints ) {
        pts.push(map.geoToXY(this.simplePoints[i].lon, this.simplePoints[i].lat));
      }

      var bMissedClockwise = false;
      var bMissedCounterClockwise = false;
      for (i = 0; i < pts.length; i++) {
        start = pts[i];
        end = pts[i == pts.length - 1 ? 0 : (i+1)];
        start.x = Math.floor(start.x);
        start.y = Math.floor(start.y);
        end.x = Math.floor(end.x);
        end.y = Math.floor(end.y);
        pt.x = Math.floor(pt.x);
        pt.y = Math.floor(pt.y);
        if (Utils.findSide(start, end, pt) < 0) {
          bMissedClockwise = true;
          break;
        }
      }
      for (i = 0; i < pts.length; i++) {
        start = pts[i];
        end = pts[i == pts.length - 1 ? 0 : (i+1)];
        start.x = Math.floor(start.x);
        start.y = Math.floor(start.y);
        end.x = Math.floor(end.x);
        end.y = Math.floor(end.y);
        pt.x = Math.floor(pt.x);
        pt.y = Math.floor(pt.y);
        if (Utils.findSide(start, end, pt) > 0) {
          bMissedCounterClockwise = true;
          break;
        }
      }
      return (!bMissedClockwise || !bMissedCounterClockwise);
    }
  });
});


  
  // this.sendAsSelectionPoly = function() {
  //   console.log("selecting cars!");
  //   // select cars
  //   var numSelected = 0;
  //   for (var i in demo.scenario.cars) {
  //     var d = false;
  //     if (demo.scenario.cars[i].id == marked) {
  //       d = true;
  //     }
  //     if (this.isInside(geoToXY(demo.scenario.cars[i].geo), d)) {
  //       numSelected++;
  //       markCar(demo.scenario.cars[i].id);
  //     }
  //   }
  // }

  

